package avicit.bdp.dms.tdm.utils;

import avicit.platform6.api.system.SysOrgClient;
import avicit.platform6.api.system.SysUserDeptPositionClient;
import avicit.platform6.api.sysuser.dto.SysDept;
import avicit.platform6.api.sysuser.dto.SysOrg;
import javax.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SysOrgUtils {

  @Autowired private SysUserDeptPositionClient sysUserDeptPositionClient;

  @Autowired private SysOrgClient sysOrgClient;

  /**
   * 根据用户ID获取当前一级单位
   *
   * @param userId 用户ID
   * @return String
   */
  public SysOrg getOrgByUserId(@NotBlank String userId) {
    SysOrg sysOrg = new SysOrg();
    // 获取userId获取用户直属部门
    SysDept chiefDept = sysUserDeptPositionClient.getChiefDeptBySysUserId(userId);
    if (chiefDept != null) {
      sysOrg = sysOrgClient.getSysOrgBySysOrgId(chiefDept.getOrgId());
      if (sysOrg != null) {
        return sysOrg;
      }
    }
    return sysOrg;
  }

  /**
   * 根据组织ID获取组织名称
   *
   * @param sysOrgId 组织ID
   * @return String
   */
  public String getSysOrgNameBySysOrgId(@NotBlank String sysOrgId, String sysLanguageCode) {
    return sysOrgClient.getSysOrgNameBySysOrgId(sysOrgId, sysLanguageCode);
  }
}
